const router = require('koa-router')()
const path = require('path')
const uploadFile=require('../utils/upload')


router.get('/', async (ctx, next) => {
   ctx.body={
    status:0,
    msg:"请求返回"
  }
})

//上传接口 上传文件 
router.post('/upload',async (ctx,next)=>{
  const { isImg, fileType } = ctx.query //获取图片数据
  const serverFilePath=path.join(__dirname,'../public/upload-files')
  const res=await uploadFile(ctx,{
    fileType: fileType || 'myUpload', // common or album
		path: serverFilePath,
		isImg: !!isImg
  })
  ctx.body={
    file:"feewl"
  }
  console.log(res,"res");
  // handleRes(ctx, next, res)
})

function handleRes(ctx,next,res){
  if(ctx.status===0){
    ctx.body=res
  }else{
    ctx.status = res.httpCode
		ctx.body = res
  }

}

router.get('/json', async (ctx, next) => {
  console.log(ctx.cookies.get('sessionId'))
  ctx.body = {
    title: 'koa2 json'
  }
})

module.exports = router
